package {
	import flash.utils.ByteArray;
	import mx.collections.ArrayCollection;

	/**
	 * @author Ritm
	 */
	 
	import flash.desktop.NativeApplication;
	import flash.filesystem.*;
    import flash.net.FileReferenceList;
	import flash.net.*;
	import flash.events.*;
	import mx.rpc.xml.SimpleXMLDecoder;
	import flash.xml.XMLDocument;
	import mx.rpc.xml.SimpleXMLEncoder;
	import mx.utils.XMLUtil;
	import mx.utils.ObjectUtil;
	import flash.xml.XMLNode;

	import mx.controls.Alert;

	public class parseFileCom {
		 
		var xmlDoc:XMLDocument = new XMLDocument();
		var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
		
		[Bindable]
		//public var data;
		
		//for client flex web
		public var data = {
			
			GPRS_SIM : {
				index 		: 1,
				data : {
					count		: 2,
					struct : {
						len : [16,32,16,16]
					}
				}
			},
			
			GPRS_COMPR : {
				index 		: 2,
				data : {
					count		: 1,
					struct : {
						len : [1]
					}
				}
			}
		};
		
		
		public function parseFileCom(){
				
		}



		
		
		
		public function open(name = null, funWhenOpen = null /*for web flex*/){
			
			
			if (name){
				  try{		
					var f:File = new File(name); 
					var fs:FileStream = new FileStream();
			
					fs.open(f, FileMode.READ);
					var s = fs.readUTFBytes(fs.bytesAvailable);
					fs.close();
					
				  }catch(e){
					  Alert.show('Error open file : ' + name);
					  return 0;
				  }
					
					return s;
					
			} else {
				//open for flex web
					var fr:FileReference = new FileReference();
				
					var loadCompleteHandler = function(e){
					fr.removeEventListener(Event.COMPLETE, loadCompleteHandler);
					
						funWhenOpen(fr.data.toString());
						
					};
				
					var selectHandler = function(e){
						fr.removeEventListener(Event.SELECT, selectHandler); 
						fr.addEventListener(Event.COMPLETE, loadCompleteHandler); 
						fr.load(); 
					};
					
					fr.addEventListener(Event.SELECT, selectHandler); 
					var fileFilter:FileFilter = new FileFilter("XML: (*.xml)", "*.xml");
					fr.browse([fileFilter,new FileFilter("Все (*.*)", "*.*")]); 
			}
			
			
		}
		
		
		
		
		public function parseData(s){
				
				if (!s) return;
				
				xmlDoc.parseXML(s);
				var resultObj:Object = decoder.decodeXML(xmlDoc);
			
				try {			
					for each(var p in resultObj){
	               		if (!p.data.struct[0]){
	               			 var el = p.data.struct.el;
	               			 if (!el) el = '';
	               			 p.data.struct[0] = {el : '0' in el ? el : [el]}; 
	               			 delete p.data.struct.el;	
	               		}
	               		
	               		for (var i in p.data.struct){
	               			//my1flex.dumpl(p.data.struct[i].el);
		               		if (!p.data.struct[i].el) p.data.struct[i].el = '';
		               		//my1flex.dumpl(p.data.struct[i],i + '  ppppppppp');
	               			if (!('0' in p.data.struct[i].el)){
	               				//my1flex.dumpl(p.data.struct[i]);
	               				p.data.struct[i].el = [p.data.struct[i].el];
	               				//my1flex.dumpl(p.data.struct[i]);
	               				 //p.data.struct.el[i] = [p.data.struct.el];
	               				//p2 = [p2.el]; 
	               			 	//delete p2.el;
	               			}
	               		}
	               		
					}
				}catch(e){
			  		Alert.show('Error parse :' + e.message);
			 }	
			 		
            //my1flex.dumpl(resultObj,'ddddddddddddddd');			 
			return resultObj;
		}
		
		
		
		public function parseShceme(s){
			
				if (!s) return;
				
				xmlDoc.parseXML(s);
				var resultObj:Object = decoder.decodeXML(xmlDoc);
				//my1flex.dumpl(resultObj);
				//return;
				
			  try{	
				for each(var p in resultObj){
					
					if (!p.index){
						Alert.show('Отсутсвует индекс у команды');
						return;
					}
					
               		if (!p.data.struct.el[0]){
               		//if (!('0' in p.data.struct.el)){
               			 if (!p.data.struct.el.len) p.data.struct.el.len = 1;
               			 p.data.struct.el = [p.data.struct.el];
               		}
               		
               		if (!p.data.count) p.data.count = 1; 
					p.data.len = [];
					for each(var n in p.data.struct.el)  p.data.len.push(n.len ? n.len : 1);
				}
			  }catch(e){
			  		Alert.show('Error parse :' + e.message);
			  }			
			  
				//my1flex.dumpl(resultObj);
				 		
			return resultObj;		
		}
	
	
	private function objectToXML(obj:Object):XML 
		{
			var qName:QName = new QName("root");
			var xmlDocument:XMLDocument = new XMLDocument();
			var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
			var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
			var xml:XML = new XML(xmlDocument.toString());
 
			return xml;
		}
		
		
	
	public function writeXML(data:ArrayCollection){
				
				var fr:FileReference = new FileReference();
				var a:ByteArray = new ByteArray();
				a.writeUTFBytes(objectToXML(data));
				fr.save(a, 'log.xml'); 
					
			
		}
		
		
	
	
	}

}
